package com.yao.image;

import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.request.RequestOptions;
import com.yao.R;

public class GlideUtil {
    protected final static int DEFAULT_PLACEHOLDER_RES_ID = R.drawable.shape_placeholder_2;   //默认占位图

    private static RequestOptions requestOptions(int resId) {
        return requestOptions(resId, null);
    }

    private static RequestOptions requestOptions(int resId, Transformation transformation) {
        RequestOptions requestOptions = new RequestOptions();
        if (transformation != null) {
            requestOptions.transform(transformation);
        }
        requestOptions.placeholder(resId).error(resId);
        return requestOptions;
    }

    public static void loadImage(ImageView imageView, Object url, RequestOptions requestOptions) {
        loadImage(imageView, url, requestOptions, null);
    }

    private static void loadImage(ImageView imageView, Object url, RequestOptions requestOptions, Float sizeMultiplier) {
        RequestBuilder requestBuilder = Glide.with(imageView.getContext())
                .load(url)
                .apply(requestOptions);
        if (sizeMultiplier != null) {
            requestBuilder.thumbnail(sizeMultiplier);
        }
        requestBuilder.into(imageView);
    }

    //普通
    public static void loadImage(ImageView imageView, Object url) {
        loadImage(imageView, url, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadImage(ImageView imageView, Object url, int placeholderResId) {
        loadImage(imageView, url, requestOptions(placeholderResId));
    }


}
